package com.witte.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.witte.entity.DdData;
import com.witte.entity.DdShow;
import com.witte.entity.RespBean;
import com.witte.service.DdService;

@Controller
@ResponseBody
@RequestMapping(value="/api/dd")
@CrossOrigin
public class DdController {
	
	@Autowired
	DdService dd;
	
	
	@GetMapping(value = "/get/{type}")
	public RespBean getData(@PathVariable("type") String type) {//接口函数
		List<DdShow> list=dd.selectByType(type);
		RespBean rs= RespBean.ok("ok",list);
		return rs;		
	}
	
	@GetMapping(value = "/getParentIdData/{parentId}")
	public RespBean getParentIdData(@PathVariable("parentId") int parentId) {//接口函数
		List<DdData> list=dd.selectByParentId(parentId);
		RespBean rs= RespBean.ok("ok",list);
		return rs;
	}
	
	@PostMapping(value = "/add")
	public RespBean add(@RequestBody DdData data) {//接口函数
		int rs=dd.add(data);
		RespBean respBean=rs>0? RespBean.ok("ok"):RespBean.ok("error");
		return respBean;
	}
	
	@PostMapping(value = "/edit")
	public RespBean edit(@RequestBody DdData data) {//接口函数
		System.out.println(data.getId());
		int rs=dd.edit(data);
		RespBean respBean=rs>0? RespBean.ok("ok"):RespBean.ok("error");
		return respBean;
	}
	
	@PostMapping(value = "/delete")
	public RespBean delete(@RequestBody DdData data) {//接口函数
		int rs=dd.delete(data);
		RespBean respBean=rs>0? RespBean.ok("ok"):RespBean.ok("error");
		return respBean;
	}
	
}
